package com.xiangxiangjd.controller;

import cn.afterturn.easypoi.word.WordExportUtil;
import com.xiangxiangjd.controller.helper.$;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.xiangxiangjd.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xiangxiangjd.service.ReturnDateNoService;
import com.xiangxiangjd.bean.ReturnDateNo;
import com.xiangxiangjd.controller.model.ApiResultModel;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 归还航材批次 前端控制器
 * </p>
 *
 * @author 肖守凯
 * @since 2021-11-08
 */
@Controller
@RequestMapping("/api/returnDateNo")
@Api(tags = "归还批次表")
public class ReturnDateNoController extends BaseController{







    /** 分页查询ReturnDateNo
     * @param page  页码
     * @param limit  查几条
     * @param returnDateNo  前端传进的参数
     * @return 返回统一分页信息
     */
    @PostMapping(value = "/admin/findReturnDateNoByPage")
    @ResponseBody
    @ApiOperation(value = "归还批次查询", notes = "归还批次查询")
    @PreAuthorize("hasRole('ROLE_USER')")
    public ResultPage findReturnDateNoByPage(Integer page, Integer limit,ReturnDateNo returnDateNo){

             return returnDateNoService.findReturnDateNoByPage(page,limit,returnDateNo);
    }

    /** 添加ReturnDateNo
     * @param returnDateNo  前端传进的参数
     * @return 返回统一信息
     */
    @PostMapping(value = "/admin/addReturnDateNo")
    @ResponseBody
    @ApiOperation(value = "添加归还批次", notes = "添加归还批次")
    @PreAuthorize("hasRole('ROLE_USER')")
    public ApiResultModel saveReturnDateNo(@RequestBody ReturnDateNo returnDateNo){

             return returnDateNoService.addReturnDateNo(returnDateNo);
    }

    /** 编辑ReturnDateNo
     * @param returnDateNo  前端传进的参数
     * @return 返回统一信息
     */
    @PostMapping(value = "admin/editReturnDateNo")
    @ResponseBody
    public ApiResultModel editReturnDateNo(ReturnDateNo returnDateNo  ){
             return returnDateNoService.editReturnDateNo(returnDateNo);
    }

    /** 删除ReturnDateNo
     * @param ids  前端传进的ID
     * @return 返回统一信息
     */
    @PostMapping(value = "admin/delReturnDateNo")
    @ResponseBody
    public ApiResultModel delReturnDateNo(@RequestParam("datas") ArrayList ids){


             return returnDateNoService.delReturnDateNos(ids);
    }


    /**
     * 归还审批成功
     * @param
     * @return
     */
    @ApiOperation(value = "归还审批", notes = "归还审批")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping(value = "returnApprove")
    @ResponseBody
    public ApiResultModel returnApprove(@RequestParam("returnDateNoId") Integer returnDateNoId,@RequestParam("returnDateNoState") String returnDateNoState,String returnForFailure){

        return returnDateNoService.returnApprove(returnDateNoId,returnDateNoState,returnForFailure);

    }

    /**
     * 归还详情
     * @param
     * @return
     */
    @ApiOperation(value = "归还单号详情", notes = "归还单号详情")
    @PreAuthorize("hasRole('ROLE_USER')")
    @PostMapping(value = "returnInfo")
    @ResponseBody
    public ApiResultModel returnInfo(@RequestParam("returnDateNoId") Integer returnDateNoId){

        ReturnDateNo returnDateNo =returnDateNoService.returnApprove(returnDateNoId);

        return $.success(returnDateNo);

    }


    /**
     * 航材申领报告表
     */
    @GetMapping(value = "/improt")
    @ApiOperation(value = "归还 word 导出", notes = "归还 word 导出")
   // @PreAuthorize("hasRole('ROLE_USER')")
    public void downDocClaimDateNo(@RequestParam("returnDateNoId") Integer returnDateNoId) throws Exception {


        Map map=returnDateNoService.downDocReturnDateNo(returnDateNoId);


        XWPFDocument doc = WordExportUtil.exportWord07("word/ReturnExcel.docx", map);

        doc.enforceReadonlyProtection();

        String fileName = System.currentTimeMillis() + ".docx";
        HttpServletResponse response = getResponse();

        response.reset();
        response.setContentType("application/msword");
        String dispositionValue = "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-disposition", dispositionValue);
        response.setCharacterEncoding("UTF-8");

        OutputStream output = response.getOutputStream();
        doc.write(output);
        output.close();

    }

}
